/***************************************************************************
 *   Copyright (C) 2006 by Autostop team                                   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "autopackets.h"




// ===========================================================================
//                                 GAutoPacket
// ===========================================================================


// ------------------------------ GAutoPacket --------------------------------
// Packet factory. Creates packet with type specified in header.
// If type is uknknown, creates UPNUnnown packet
GAutoPacket* GAutoPacket::createPacket( const GHeader& header )
{
	switch( header.type() )
	{
		case PACKET_PARAMS:
			return new GAutoParams();
			
		case PACKET_XML_CONFIG:
			return new GAutoXmlConfig();
			
		case PACKET_MARKERS:
			return new GAutoMarkers();
			
		case PACKET_ROI:
			return new GAutoRoi();
			
		case PACKET_REQUEST:
			return new GAutoRequest();
			
		case PACKET_REPLY_FRAME:
			return new GAutoReplyFrame();
			
		case PACKET_REPLY_XML_CONFIG:
			return new GAutoReplyXmlConfig();
			
		default:
			return new GAutoUnknown();
	}
}
// ---------------------------------------------------------------------------


// ------------------------- GAutoPacket -------------------------------------
/// Creates approproate header using packet type info
GHeader GAutoPacket::header() const
{
	GHeader header;
	header.setType( packetType() );

	return header;
}
// ---------------------------------------------------------------------------




// ===========================================================================
//                                 GAutoRequest
// ===========================================================================


// ------------------------- GAutoRequest ------------------------------------
/// Request packer constructor. Requires request type and unique id,
/// used to identify associate requestwith its answer
GAutoRequest::GAutoRequest( GAutoRequest::RequestType type, quint32 id )
{
	_requestType	= type;
	_requestId		= id;
}
// ---------------------------------------------------------------------------


// ------------------------- GAutoRequest::write -----------------------------
/// Serializes request into stream
void GAutoRequest::writeData( QDataStream & out ) const
{
	out << (quint16)_requestType;
	out << _requestId;
}
// ---------------------------------------------------------------------------


// ------------------------- GAutoRequest::read ------------------------------
/// Regenrates object from stream
void GAutoRequest::readData( QDataStream & in )
{
	quint16 type;
	in >> type;
	in >> _requestId;

	_requestType = (RequestType) type;
}
// ---------------------------------------------------------------------------




// ===========================================================================
//                             GAutoParams
// ===========================================================================


// ------------------------- GAutoParams::write ------------------------------
/// Serializes request into stream
void GAutoParams::writeData( QDataStream & out ) const
{
	out << _params;
}
// ---------------------------------------------------------------------------


// ------------------------- GAutoParams::read -------------------------------
/// Regenrates object from stream
void GAutoParams::readData( QDataStream & in )
{
	in >> _params;
}
// ---------------------------------------------------------------------------




// ===========================================================================
//                             GAutoReplyFrame
// ===========================================================================


// ------------------------- GAutoReplyFrame::write --------------------------
/// Serializes packet into stream
void GAutoReplyFrame::writeData( QDataStream & out ) const
{
	out << _requestId;
	out << _width;
	out << _height;
	out << _image;
	out << _timestamp;
}
// ---------------------------------------------------------------------------


// ------------------------- GAutoReplyFrame::read ---------------------------
/// Regenrates object from stream
void GAutoReplyFrame::readData( QDataStream & in )
{
	in >> _requestId;
	in >> _width;
	in >> _height;
	in >> _image;
	in >> _timestamp;
}
// ---------------------------------------------------------------------------




// ===========================================================================
//                              GAutoXmlConfig
// ===========================================================================


// ------------------------- GAutoXmlConfig::write ---------------------------
/// Serializes packet into stream
void GAutoXmlConfig::writeData( QDataStream & out ) const
{
	out << _data;
}
// ---------------------------------------------------------------------------


// ------------------------- GAutoXmlConfig::read ----------------------------
/// Regenrates object from stream
void GAutoXmlConfig::readData( QDataStream & in )
{
	in >> _data;
}
// ---------------------------------------------------------------------------




// ===========================================================================
//                              GAutoReplyXmlConfig
// ===========================================================================


// ------------------------- GAutoReplyXmlConfig::write ----------------------
/// Serializes packet into stream
void GAutoReplyXmlConfig::writeData( QDataStream & out ) const
{
	out << _requestId;
	out << _data;
}
// ---------------------------------------------------------------------------


// ------------------------- GAutoReplyXmlConfig::read -----------------------
/// Regenrates object from stream
void GAutoReplyXmlConfig::readData( QDataStream & in )
{
	in >> _requestId;
	in >> _data;
}
// ---------------------------------------------------------------------------




// ===========================================================================
//                              GAutoMarkers
// ===========================================================================


// ------------------------- GAutoMarkers::write -----------------------------
/// Serializes packet into stream
void GAutoMarkers::writeData( QDataStream & out ) const
{
	quint32 width = _markers->width;
	quint32 height = _markers->height;
	QByteArray data( width * height, 0 );
	
		// fill data with markers image data 
	for( quint32 y = 0; y < height; y++ )
	{
		for( quint32 x = 0; x < width; x++ )
		{
			data[ y * height + x ] = image_pixel( _markers, x, y );
		}
	}
	
	out << width;
	out << height;
	out << data;
}
// ---------------------------------------------------------------------------


// ------------------------- GAutoMarkers::read ------------------------------
/// Regenrates object from stream
void GAutoMarkers::readData( QDataStream & in )
{
	quint32 width = 0;
	quint32 height = 0;
	QByteArray data;
	
	in >> width;
	in >> height;
	in >> data;
	
		// extract data to markers image 
	_markers = create_image( width, height );	
	for( quint32 y = 0; y < height; y++ )
	{
		for( quint32 x = 0; x < width; x++ )
		{
			image_pixel( _markers, x, y ) = data[ y * height + width ];
		}
	}
}
// ---------------------------------------------------------------------------




// ===========================================================================
//                              GAutoRoi
// ===========================================================================


// ------------------------- GAutoRoi::write ---------------------------------
/// Serializes packet into stream
void GAutoRoi::writeData( QDataStream & out ) const
{
	quint32 width = _roi->width;
	quint32 height = _roi->height;
	QByteArray data( width * height, 0 );
	
		// fill data with markers image data 
	for( quint32 y = 0; y < height; y++ )
	{
		for( quint32 x = 0; x < width; x++ )
		{
			data[ y * height + x ] = image_pixel( _roi, x, y );
		}
	}
	
	out << width;
	out << height;
	out << data;
}
// ---------------------------------------------------------------------------


// ------------------------- GAutoRoi::read ----------------------------------
/// Regenrates object from stream
void GAutoRoi::readData( QDataStream & in )
{
	quint32 width = 0;
	quint32 height = 0;
	QByteArray data;
	
	in >> width;
	in >> height;
	in >> data;
	
		// extract data to markers image 
	_roi = create_image( width, height );	
	for( quint32 y = 0; y < height; y++ )
	{
		for( quint32 x = 0; x < width; x++ )
		{
			image_pixel( _roi, x, y ) = data[ y * height + width ];
		}
	}
}
// ---------------------------------------------------------------------------




// eof
